#include "TUI.h" 





TPanelItem* TUIExpand::createUI(xml_node<> *){

    return NULL;
}

TPanelItem* TUIExpand::createUI(const string&){
    
    return NULL;
}
// singleton stuff
static TUI s_SharedTUI;
 
extern const char* cocos2dVersion(void);

TUI* TUI::sharedDirector(void)
{
    static bool s_bFirstUseDirector = true;
    if (s_bFirstUseDirector)
    {
        s_bFirstUseDirector = false;
        s_SharedTUI.init();
    }
    
    return &s_SharedTUI;
}


TPanelItem* TUI::createUI(const string& itemName){
    
    TPanelItem* ret=NULL;
    if(itemName==T_SCALE9_NAME){
        ret=TScale9::create(); 
    }
    else if(itemName==T_BUTTON_NAME){
        ret=TButton::create(); 
    }
    else if(itemName==T_IMAGE_NAME){
        ret=TImage::create();
    }
    else if(itemName==T_LABEL_NAME){
        ret=TLabel::create();
    }
    else if(itemName==T_TAB_NAME){
        ret=TTab::create();
    }
    else if(itemName==T_SCROLL_NAME){
        ret=TScrollPanel::create();
    }
    else if(itemName==T_PAGE_NAME){
        ret=TPage::create();
    }else if(itemName==T_PANEL_NAME){
        ret=TPanel::create();
    }else if(itemName==T_BAR_NAME){
        ret=TBar::create();
    }else if(itemName==T_PANE_ITEM_NAME){
        ret=TPanelItem::create();
    }else if(itemName==T_COUNT_NAME){
        ret=TCount::create();
    }
    else if(itemName==T_VBOX_NAME){
        ret=TVBox::create();
    }else if(itemName==T_ROCKER_NAME){
        ret=TRocker::create();
    }else if(itemName==T_CHECK_NAME){
        ret=TCheckBox::create();
    }else if(itemName==T_EDIT_BAR_NAME){
        ret=TEditBar::create();
    }else if(itemName==T_INPUT_NAME){
        ret=TInput::create();
    }else if(itemName==T_RADIO_NAME){
        ret=TRadio::create();
    }else if(itemName==T_ACCORDION_NAME){
        ret=TAccordion::create();
    }else if(itemName==T_DROPDOWN_NAME){
        ret=TDropdown::create();
    }else if(itemName==T_STROKE_LABEL_NAME) {
		ret=TStrokeLabel::create();
	}
    else {
        if(m_pExpand){
            ret=m_pExpand->createUI(itemName);
            
        }
    }
    return ret;
    
}


TPanelItem* TUI::createUI(xml_node<> * pItem){
    string itemName=  pItem->name() ;
    string id,type,event,mask;
    int ttag=0,maskType=0;
    readAttrString(pItem, "id", id);
    readAttrString(pItem, "event", event);
    readAttrString(pItem, "tcomName", type);
    readAttrString(pItem, "mask", mask); 
    ttag=readAttrInt(pItem, "tag");
    maskType=readAttrInt(pItem, "maskType");
    if(type.size()!=0){
        itemName=type;
    }
    TPanelItem* ret=createUI(itemName);
    
    
    if(ret){
        ret->tEvent=event;
        if(ttag!=0){ 
            ret->setTag(ttag);
        }
        ret->tMaskType=maskType;
        ret->tMask=mask;
        ret->tType=itemName;
        ret->tId=id;
    }
    
    return ret;
}

void TUI::setExpand(TUIExpand* exp){
    m_pExpand=exp;
}

void TUI::init(){
    
}

TUI::TUI(void)
{
    m_pExpand=NULL;
}


xml_document<>* TUI::getDoc(const string& name){
    
   if( docs.find(name)!=docs.end())
   {
       return docs[name];
   }
    
    return NULL;
    
}
void TUI::addDoc(const string& name,xml_document<>* doc,unsigned char * data){
    if(doc)
    {
        docs[name]=doc;
    }
    
    datas.push_back(data);
}

TUI::~TUI(void){
    
    
    map<string,xml_document<>*>::iterator it=docs.begin();
    
    for( ;it!=docs.end() ;it++){
        SAFE_DEL(it->second);
    }
    docs.clear();
    
    for(uint32 i=0;i<datas.size();i++){
        SAFE_DEL(datas[i]);
    }
    datas.clear();
}

